#!/bin/bash

# 编译平台
# "aarch64 arm"
ARCH=""
# 目标Android版本
API="24"
# 支持的CPU架构 armv7-a armv8-a
CPU="armv8-a"
# 动态度/DYNAMIC  静态库/STATIC
LIBRARY="DYNAMIC"
# so库输出目录
OUTPUT="FFMpeg-Android$API"
# NDK的路径
NDK="/Users/ilongge/PersonalCode/AndroidNDK/android-ndk-r22b"
if [ ! -r "$NDK" ]; then
	echo "请配置NDK路径！！！"
	exit 1
fi
# 编译工具链路径
TOOLCHAIN="$NDK/toolchains/llvm/prebuilt/darwin-x86_64"
# 编译环境
SYSROOT="$TOOLCHAIN/sysroot"
# 编译参数
CONFIGURE_FLAGS=""

if [ $LIBRARY = "DYNAMIC" ]; then
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-shared"
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-static"
elif [ $LIBRARY = "STATIC" ]; then
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-static"
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-shared" 
	sed -i "" "s/SLIBNAME_WITH_MAJOR='\$(SLIBNAME).\$(LIBMAJOR)'/SLIBNAME_WITH_MAJOR='\$(SLIBPREF)\$(FULLNAME)-\$(LIBMAJOR)\$(SLIBSUF)'/" configure
	sed -i "" "s/LIB_INSTALL_EXTRA_CMD='\$\$(RANLIB) \"\$(LIBDIR)\\/\$(LIBNAME)\"'/LIB_INSTALL_EXTRA_CMD='\$\$(RANLIB) \"\$(LIBDIR)\\/\$(LIBNAME)\"'/" configure
	sed -i "" "s/SLIB_INSTALL_NAME='\$(SLIBNAME_WITH_VERSION)'/SLIB_INSTALL_NAME='\$(SLIBNAME_WITH_MAJOR)'/" configure
	sed -i "" "s/SLIB_INSTALL_LINKS='\$(SLIBNAME_WITH_MAJOR) \$(SLIBNAME)'/SLIB_INSTALL_LINKS='\$(SLIBNAME)'/" configure
fi

CROSS_PREFIX_CLANG="$TOOLCHAIN/bin"
EXTRA_CFFLAGS="-fPIC"
CROSS_PREFIX=""
LIBGCC=""
STRIP=""
LD=""
if [ $CPU = "armv8-a" ]; then
	OUTPUT="$OUTPUT-armv8a-$LIBRARY"
	ARCH="aarch64"
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-x86asm"
	CROSS_PREFIX="$CROSS_PREFIX_CLANG/$ARCH-linux-android-"
	CROSS_PREFIX_CLANG="$CROSS_PREFIX_CLANG/$ARCH-linux-android$API-"
	LIBGCC=$TOOLCHAIN/lib/gcc/$ARCH-linux-android/4.9.x/libgcc.a
	STRIP=$TOOLCHAIN/bin/$ARCH-linux-android-strip
	LD=$TOOLCHAIN/$ARCH-linux-android/bin/ld
elif [ $CPU = "armv7-a" ]; then
	OUTPUT="$OUTPUT-armv7a-$LIBRARY"
	ARCH="arm"
	CROSS_PREFIX="$CROSS_PREFIX_CLANG/arm-linux-androideabi-"
	CROSS_PREFIX_CLANG="$CROSS_PREFIX_CLANG/armv7a-linux-androideabi$API-"
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-asm"
	LIBGCC=$TOOLCHAIN/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a
	STRIP=$TOOLCHAIN/bin/arm-linux-androideabi-strip 
	LD=$TOOLCHAIN/arm-linux-androideabi/bin/ld
fi

CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-cross-compile"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-debug"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-programs"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-doc"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-ffplay"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-ffprobe"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-symver"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-ffmpeg"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-avdevice"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-pic"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-neon"

echo "./configure"
echo "    --prefix=$OUTPUT"
echo "    --target-os=android"
echo "    --arch=$ARCH"
echo "    --cpu=$CPU"
for FLAG in $CONFIGURE_FLAGS; do
	echo "    "$FLAG
done
echo "    --sysroot=$SYSROOT"
echo "    --cross-prefix=$CROSS_PREFIX""clang"
echo "    --cross-prefix-clang=$CROSS_PREFIX_CLANG""clang"
echo "    --extra-cflags=$EXTRA_CFFLAGS"

./configure \
	--prefix=$OUTPUT \
	--target-os=android \
	--arch=$ARCH \
	--cpu=$CPU \
	$CONFIGURE_FLAGS \
	--sysroot=$SYSROOT \
	--cross-prefix=$CROSS_PREFIX \
	--cross-prefix-clang=$CROSS_PREFIX_CLANG \
	--extra-cflags=$EXTRA_CFFLAGS || exit 1
#获取机器CPU核心数 尽可能加快编译
THREAD_COUNT=$(sysctl -n machdep.cpu.thread_count)
echo "make -j $THREAD_COUNT install"
make -j$THREAD_COUNT install || exit 1

echo "开始清理编译生成的中间文件"
make clean
echo "清理完成"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "+  Congratulations ! ! !                            +"
echo "+  Build FFMpeg-Android Success ! ! !               +"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"